package com.chancingpack.service.impl;

import com.chancingpack.dal.dao.WxBookDao;
import com.chancingpack.dal.model.WxBook;
import com.chancingpack.service.WxBookService;
import com.chancingpack.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.*;

import org.apache.log4j.Logger;

/**
 * Created by Administrator on 2017/7/29.
 */
@Service
public class WxBookServiceImpl implements WxBookService{
    @Autowired
    WxBookDao dao;
    private static Logger logger = Logger.getLogger(WxBookService.class);

    /**
    * 添加到购物车
    *
    * */
    public CommonResult addBook(WxBook record) {
        CommonResult result = new CommonResult();
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("custId",record.getCustId());
        map.put("archId",record.getArchId());
        List<Map<String,Object>> bookList = dao.selectByCustId(map);
        try {
            if (bookList.size()!=0){
                result.setResultMsg("该产品您已经加入购物车！");
            } else {
                boolean success = false;
                success= dao.insertBook(record);
                result.setIsSuccess(success);
                result.setResultMsg("添加成功");
            }
        } catch (Exception e) {
            logger.error("加入购物车失败", e);
        }
        return result;

    }

    /**
    * 通过 custId 和 archId 来查找
    * 该用户添加购物车的产品
    * */
    public List<Map<String, Object>> queryByCustId(Map<String,Object> map) {
        return dao.selectByCustId(map);
    }

    /**
       * 通过 bookId 删除购物车里的产品
       * */
    public CommonResult deleteBook(int bookId) {
        CommonResult result = new CommonResult();
        try {
                boolean success = false;
                success= dao.deleteByPrimaryKey(bookId);
                result.setIsSuccess(success);
                result.setResultMsg("删除成功");

        } catch (Exception e) {
            logger.error("操作失败", e);
        }
        return result;
    }
    /**
     * 下单
     *
     * */
    public CommonResult insertsgs(List<Map<String, Object>> mapList,List<Map<String,Object>> mapListJson) {
        CommonResult result = new CommonResult();

        try {
            boolean success = false;
            for (int i =0;i<mapList.size();i++) {
                success = dao.insertsgslecompact(mapList.get(i));
                if (success) {
                    success = insertdetail(mapList, mapListJson, i);
                    if (success) {
                        success = dao.deleteByPrimaryKey(Integer.parseInt(mapListJson.get(i).get("bookId").toString()));
                        result.setIsSuccess(success);
                        result.setResultMsg("下单成功");
                    }
                }
            }
        } catch (Exception e) {
            logger.error("操作失败", e);
        }
        return result;
    }
/**
 * 通过客户id 查找客户资料
* */
    public List<Map<String, Object>> querysgbasCustInfo(String custId) {
        return dao.selectsgbasCustInfo(custId);
    }
    /**
     * 通过承揽方id 查找承揽方资料
     * */
    public List<Map<String, Object>> querysgBasCorpInfo(String purveCode) {
        return dao.selectsgBasCorpInfo(purveCode);
    }
    /**
     * 生成合同编号
     * */
    public String wxCompaCode() {
        Calendar now = Calendar.getInstance();
        String wx="WX-";
        String year=(String.valueOf(now.get(Calendar.YEAR))).substring(2,4)+"-";
        DecimalFormat YMD = new DecimalFormat("00");
        int month1=Integer.valueOf(now.get(Calendar.MONTH) + 1).intValue();
        String month = YMD.format(month1)+"-";
        int date1=Integer.valueOf(now.get(Calendar.DAY_OF_MONTH)).intValue();
        String date = YMD.format(date1)+"-";
        String CompaCode=wx+year+month+date;

        List<String> CodeList = dao.selectsgSleCompact(CompaCode);
        String str="";
        if(CodeList.size()==0){
            DecimalFormat f = new DecimalFormat("00000");
            int i=0;
             str = f.format(i);
            System.out.println(str+"------");
        }else{
            String b=CodeList.get(0).substring(CodeList.get(0).length()-5,CodeList.get(0).length());
            DecimalFormat f = new DecimalFormat("00000");
            int i=Integer.parseInt(b)+1;
           str = f.format(i);
        }
        CompaCode=CompaCode+str;

        return CompaCode;
    }
/**
 * 查找最大的销售订单表系统编号
 * */
    public String selectcompaId() {
        return dao.selectcompaId();
    }
    /**
     * 向销售订单明细表添加数据
     * */
    public boolean insertdetail(List<Map<String, Object>> mapList,List<Map<String,Object>> mapListJson,int num) {
        Map<String,Object> map = new HashMap<String, Object>();
        Boolean detail = false;
        int detail_id=dao.selectdetailId();
            Map<String,Object> sgsmap=new HashMap<String, Object>();
            map.put("bookId",mapListJson.get(num).get("bookId"));
            List<Map<String,Object>> bookList= dao.selectByCustId(map);
            detail_id=detail_id+1;
            sgsmap.put("detail_id",detail_id);
            sgsmap.put("compa_id",mapList.get(num).get("compa_id"));
            sgsmap.put("compa_code", mapList.get(num).get("compa_code"));
            sgsmap.put("prod_name", mapList.get(num).get("prod_name"));
            sgsmap.put("prod_spec", bookList.get(0).get("prod_spec_desc"));
            sgsmap.put("prod_mate", bookList.get(0).get("mstru_name"));
            sgsmap.put("make_way", bookList.get(0).get("make_name"));
            sgsmap.put("sly", bookList.get(0).get("mater_ply"));
            sgsmap.put("qty", mapListJson.get(num).get("num"));
            sgsmap.put("cost_id", bookList.get(0).get("arch_id"));
            sgsmap.put("order_date", mapListJson.get(num).get("dateTime"));
            sgsmap.put("order_unit", bookList.get(0).get("UNIT_NAME"));
            sgsmap.put("cust_pdex", bookList.get(0).get("prod_exter"));
            detail = dao.insertsgSleCompactDetail(sgsmap);


        return detail;

    }


}
